# _*_ coding:utf-8 _*_
# @Time  : 2022.07.12
# @Author: zizlee
import datetime
import json
import time
import pandas as pd
import requests
from urllib3 import disable_warnings
disable_warnings()


def get_indexes(kw):
    url = 'https://210.13.218.130:9000/v1/datalib/table/'
    prams = {
        'kw': kw,
        'page': 1,
        'page_size': 100
    }
    r = requests.get(url, params=prams, verify=False)
    data = r.json()['data']['records']
    return data


def is_index_exist(plat, sid):
    url = 'https://210.13.218.130:9000/v1/datalib/table/exists/'
    params = {'plat': plat, 'sid': sid}

    r = requests.get(url, params, verify=False)
    return r.json()['data']


def generate_customer_index_list(variety, variety_en):
    index_list = []
    for name in ['主力收盘价', '主力结算价', '品种现货价', '基差']:
        index_list.append({
            'name_zh': f'{name}:{variety}',
            'frequency': '日',
            'unit': '元/吨',
            'startdate': '2023-09-15',
            'variety': variety_en,
            'formula': '',
            'state': 1,
            'is_open': 1,
            'note': '',
            'raw_source': '郑商所',
            'source_plat': 'rj',
            'three_plat': '',
        })

    for name in ['主力成交量', '主力持仓量', '期货成交量', '期货持仓量', '期货前20名买单量', '期货前20名卖单量', '期货前20名净持仓', '期货前20名成交量',]:
        index_list.append({
            'name_zh': f'{name}:{variety}',
            'frequency': '日',
            'unit': '手',
            'startdate': '2023-09-15',
            'variety': variety_en,
            'formula': '',
            'state': 1,
            'is_open': 1,
            'note': '',
            'raw_source': '郑商所',
            'source_plat': 'rj',
            'three_plat': '',
        })
    for month in range(1, 13):
        index_list.append({
            'name_zh': f'合约收盘价:{variety}:{month}月',
            'frequency': '日',
            'unit': '元/吨',
            'startdate': '2023-09-15',
            'variety': variety_en,
            'formula': '',
            'state': 1,
            'is_open': 1,
            'note': '',
            'raw_source': '郑商所',
            'source_plat': 'rj',
            'three_plat': '',
        })
    return index_list


def add_new_custom_index(index_list):  # 添加新的自定义指标
    url = 'https://210.13.218.130:9000/v1/datalib/table/csm/'
    for i in index_list:
        r = requests.post(url, json=i, verify=False,
                          headers={'Ruizy-Token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIsInJvbGVfY29kZSI6OTk5OSwib3V0X3JvbGUiOjI1NSwiZXhwIjoxNjk1MDg0NzE4Ljc2OTU4Nzh9._2rICRp5mPu2iZTyIGpvFOjS2XptPUWW6w_toF2iOho'})
        print(r.json(), i['name_zh'])
        time.sleep(1)


def add_index_to_ruizhi(index_list):  # 添加到瑞智系统
    for i, row in enumerate(index_list, 1):
        url = "http://210.13.218.130:9001/v1/admin/dataindex/create/"
        r = requests.post(url, json={
            'name': row['name_zh'],
            'frequency': row['frequency'],
            'unit': row['unit'],
            'start_date': row['startdate'],
            'plat': 'ruizhi',
            'plat_id': '',
            'source': '',
            'note': ''
        }, verify=False)
        time.sleep(1)
        print(r.json())
        # str_i = '%06d' % (8696 + i)
        # print(f'{str_i},1,自定义,,{row["frequency"]},,,,,{row["variety"]},{row["id"]},,{row["name_zh"]}')


def save_value(sheet_id, datalist):
    url = 'https://210.13.218.130:9000/v1/datalib/table/update/'
    body_data = {'dataid': sheet_id, 'row_value': datalist}
    r = requests.post(url, json=body_data, verify=False)
    print(r.json())


def get_rdb_data(formula):
    r = requests.post('https://210.13.218.130:9000/v1/datalib/indexAis/formulaData/', verify=False,
                      json={'formula': formula})
    resp_data = r.json()['data']
    return resp_data['data']


if __name__ == '__main__':
    i_data = generate_customer_index_list(variety='对二甲苯', variety_en='PX')
    # add_new_custom_index(i_data)
    add_index_to_ruizhi(i_data)